package com.cloudbroker.bcs.platform.dav.middle.bizservice.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.model.Pagination;
import com.cloudbroker.bcs.common.util.StringUtil;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes.PlugsErr;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes.PlugsTypeErr;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.status.PlugsStatus;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.PlugsBizService;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.result.PlugsDavListData;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.result.PlugsDavListResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.result.PlugsResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.vo.PlugsDelForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.vo.PlugsForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.vo.PlugsListForm;
import com.cloudbroker.bcs.platform.dav.common.constant.SysContants;
import com.cloudbroker.bcs.platform.dav.middle.entity.ExtendApp;
import com.cloudbroker.bcs.platform.dav.middle.entity.Plugs;
import com.cloudbroker.bcs.platform.dav.middle.entity.PlugsType;
import com.cloudbroker.bcs.platform.dav.middle.service.ExtendAppGroupService;
import com.cloudbroker.bcs.platform.dav.middle.service.ExtendAppService;
import com.cloudbroker.bcs.platform.dav.middle.service.PlugsService;
import com.cloudbroker.bcs.platform.dav.middle.service.PlugsTypeService;

@Service
public class PlugsBizServiceImpl implements PlugsBizService {
    
    @Resource
    private PlugsService plugsService;
    
    @Resource
    private PlugsTypeService plugsTypeService;
    
    @Resource
    private ExtendAppService extendAppService;
    
    @Resource
    private ExtendAppGroupService extendAppGroupService;
    
    @Override
    public HSBCPSBackVO funcPlugsSave(PlugsForm form) {
        PlugsType type = new PlugsType();
        type.setPlugs_type_id(Long.valueOf(form.getPlugs_type_id()));
        int count = plugsTypeService.count(type);
        if (count == 0) {
            return VOUtil.genErrorResult(PlugsTypeErr.PLUGS_TYPE_NOT_EXIST, PlugsResult.class);
        }
        
        form.setPlugs_id(null);
        PlugsResult result = valid(form);
        if (!"0".equals(result.getError_no())) {
            return result;
        }
        Plugs saveDb = new Plugs();
        saveDb.setPlugs_type_id(Long.valueOf(form.getPlugs_type_id()));
        saveDb.setLink(form.getLink());
        saveDb.setIcon(form.getIcon());
        saveDb.setName(form.getName());
        saveDb.setDescription(form.getDescription());
        saveDb.setCreater("云纪网络");
        saveDb.setPrice(form.getPrice() == null ? BigDecimal.ZERO : form.getPrice());
        saveDb.setDiscount_price(form.getDiscount_price() == null ? BigDecimal.ZERO : form.getDiscount_price());
        saveDb.setCharge_mod(Integer.valueOf(form.getCharge_mod()));
        saveDb.setCreate_time(new Date());
        saveDb.setPlugs_status(Integer.valueOf(form.getPlugs_status()));
        saveDb.setVer(form.getVer());
        saveDb.setCode(form.getCode());
        saveDb.setSort(Long.valueOf(form.getSort()));
        saveDb.setTerm_type(form.getTerm_type().endsWith(",") ? form.getTerm_type() : form.getTerm_type() + ",");
        saveDb.setContent_source(form.getContent_source());
        plugsService.save(saveDb);
        return VOUtil.setSuccessResult(result);
    }
    
    @Override
    public HSBCPSBackVO funcPlugsUpdate(PlugsForm form) {
        
        if (StringUtil.isEmpty(form.getPlugs_id())) {
            return VOUtil.genErrorResult(PlugsErr.PLUGS_NOT_EXIST, PlugsResult.class);
        }
        
        Plugs validDb = new Plugs();
        validDb.setPlugs_id(Long.valueOf(form.getPlugs_id()));
        int count = plugsService.count(validDb);
        if (count == 0) {
            return VOUtil.genErrorResult(PlugsErr.PLUGS_NOT_EXIST, PlugsResult.class);
        }
        
        PlugsResult result = valid(form);
        if (!"0".equals(result.getError_no())) {
            return result;
        }
        
        if ("0".equals(form.getPlugs_status())) {
            ExtendApp app = new ExtendApp();
            app.setPlugs_id(Long.valueOf(form.getPlugs_id()));
            app.setExtend_app_status(PlugsStatus.SALE.getVal());
            int extendAppCount = extendAppService.count(app);
            if (extendAppCount > 0) {
                return VOUtil.genErrorResult(PlugsErr.PLUGS_IS_USING, HSBCPSBackVO.class);
            }
        }
        
        Plugs updateDb = new Plugs();
        updateDb.setPlugs_id(Long.valueOf(form.getPlugs_id()));
        updateDb.setPlugs_type_id(Long.valueOf(form.getPlugs_type_id()));
        updateDb.setLink(form.getLink());
        updateDb.setIcon(form.getIcon());
        updateDb.setName(form.getName());
        updateDb.setDescription(form.getDescription());
        updateDb.setPrice(form.getPrice());
        updateDb.setDiscount_price(form.getDiscount_price());
        updateDb.setCharge_mod(Integer.valueOf(form.getCharge_mod()));
        updateDb.setPlugs_status(Integer.valueOf(form.getPlugs_status()));
        updateDb.setVer(form.getVer());
        updateDb.setCode(form.getCode());
        updateDb.setSort(Long.valueOf(form.getSort()));
        updateDb.setTerm_type(form.getTerm_type());
        updateDb.setContent_source(form.getContent_source());
        plugsService.update(updateDb);
        
        return VOUtil.setSuccessResult(result);
    }
    
    public PlugsDavListResult funcPlugsList(PlugsListForm form) {
        PlugsDavListResult result = new PlugsDavListResult();
        
        Integer request_num = StringUtil.isBlank(form.getRequest_num())
                || Integer.parseInt(form.getRequest_num()) <= 0 ? SysContants.DEFAULT_REQUSET_NUM : Integer
                .parseInt(form.getRequest_num());
        Integer page_no = StringUtil.isBlank(form.getPage_no()) || Integer.parseInt(form.getPage_no()) <= 0 ? 1
                : Integer.parseInt(form.getPage_no());
        
        Plugs plugs = new Plugs();
        if(!StringUtil.isEmpty(form.getTerm_type_num())){
            plugs.setTerm_type_num(form.getTerm_type_num());
        }
        if (!StringUtil.isEmpty(form.getName())) {
            plugs.setName(form.getName());
        }
        if (!StringUtil.isBlank(form.getPlugs_type_id())) {
            plugs.setPlugs_type_id(Long.valueOf(form.getPlugs_type_id()));
        }
        
        
        Pagination page = new Pagination();
        page.setPage_no(page_no);
        page.setPage_size(request_num);
        
        plugs.setPage(page);
        
        List<PlugsDavListData> list = plugsService.queryPlugsForWeb(plugs, page);
        
        result.setPage_no(page.getPage_no());
        result.setPage_size(page.getPage_size());
        result.setTotal_item_num(page.getTotal_item_num());
        result.newDataList().addAll(list);
        return VOUtil.setSuccessResult(result);
    }
    
    public HSBCPSBackVO funcPlugsDel(PlugsDelForm form) {
        Plugs p = new Plugs();
        p.setPlugs_id(Long.valueOf(form.getPlugs_id()));
        
        int count = plugsService.count(p);
        if (count == 0) {
            return VOUtil.genErrorResult(PlugsErr.PLUGS_NOT_EXIST, HSBCPSBackVO.class);
        }
        
        ExtendApp app = new ExtendApp();
        app.setPlugs_id(Long.valueOf(form.getPlugs_id()));
        app.setExtend_app_status(PlugsStatus.SALE.getVal());
        int extendAppCount = extendAppService.count(app);
        if (extendAppCount > 0) {
            return VOUtil.genErrorResult(PlugsErr.PLUGS_IS_USING, HSBCPSBackVO.class);
        }
        
        plugsService.delete(form.getPlugs_id());
        
        return VOUtil.genSuccessBackVo();
    }
    
    public PlugsResult valid(PlugsForm form) {
        PlugsResult result = new PlugsResult();
        
        Plugs validDb = new Plugs();
        validDb.setTerm_type(form.getTerm_type());
        int count = 0;
        
        if (StringUtil.isEmpty(form.getPlugs_id())) {
            
            validDb.setName(form.getName());
            count = plugsService.count(validDb);
            validDb.setName(null);
            if (count > 0) {
                return VOUtil.genErrorResult(PlugsErr.PLUGS_NAME_EXIST, PlugsResult.class);
            }
            
            validDb.setLink(form.getLink());
            count = plugsService.count(validDb);
            validDb.setLink(null);
            if (count > 0) {
                return VOUtil.genErrorResult(PlugsErr.PLUGS_LINK_EXIST, PlugsResult.class);
            }
            
            validDb.setCode(form.getCode());
            count = plugsService.count(validDb);
            validDb.setCode(null);
            if (count > 0) {
                return VOUtil.genErrorResult(PlugsErr.PLUGS_CODE_EXIST, PlugsResult.class);
            }
        } else {
            
            validDb.setName(form.getName());
            List<Plugs> list = plugsService.query(validDb);
            validDb.setName(null);
            if (list != null && list.size() > 0) {
                if (!list.get(0).getPlugs_id().equals(Long.valueOf(form.getPlugs_id()))) {
                    return VOUtil.genErrorResult(PlugsErr.PLUGS_NAME_EXIST, PlugsResult.class);
                }
            }
            
            validDb.setLink(form.getLink());
            list = plugsService.query(validDb);
            validDb.setLink(null);
            if (list != null && list.size() > 0) {
                if (!list.get(0).getPlugs_id().equals(Long.valueOf(form.getPlugs_id()))) {
                    return VOUtil.genErrorResult(PlugsErr.PLUGS_LINK_EXIST, PlugsResult.class);
                }
            }
            
            validDb.setCode(form.getCode());
            list = plugsService.query(validDb);
            validDb.setCode(null);
            if (list != null && list.size() > 0) {
                if (!list.get(0).getPlugs_id().equals(Long.valueOf(form.getPlugs_id()))) {
                    return VOUtil.genErrorResult(PlugsErr.PLUGS_CODE_EXIST, PlugsResult.class);
                }
            }
        }
        
        if (form.getDiscount_price().compareTo(form.getPrice()) == 1) {
            return VOUtil.genErrorResult(PlugsErr.PLUGS_DISCOUNT_CANOT_THEN_PRICE, PlugsResult.class);
        }
        return VOUtil.setSuccessResult(result);
    }
    
}
